﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Web.Global;
using Portal.Web.Framework;
using Portal.Utilities;
using Portal.AssetTn.Model;
using System.IO;
using Portal.Core;

namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AssetType : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AssetType";
            }
        }

        public override string Title
        {
            get
            {
                return "Loại hiện vật";
            }
        }


        public override WebPartServiceCls WebPartService
        {
            get
            {
                return AssetUtility.WebPartService;
            }
        }

        [AjaxPro.AjaxMethod]
        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            try
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetType));
                //Environments.CheckWebPartAllow(WebPartId, true);
                Environments.CheckSessionTimeOut(true);
                string SiteId = loadParamCls.SiteId;

                Permission.CheckPermission(
                      SiteId,
                      Environments.CurrentLoginUser.LoginName,
                      Portal.AssetTn.Security.Permission.GetPermissionId(),
                      Portal.AssetTn.Security.Permission.ePermisionType.Portal_AssetTn_Main.ToString(),
                      Portal.AssetTn.Security.Permission.eFunction.Portal_AssetTn_Admin.ToString(),
                      Portal.AssetTn.Security.Permission.GetPermissionId(),
                      null,
                      true);


                string RetHtml =
                    "<script language=javascript>\r\n" +
                    "   function ClientBack()\r\n" +
                    "   {\r\n" +
                    "       document.getElementById('divActionForm').innerHTML='';\r\n" +
                    "       document.getElementById('divMainForm').style.display='block';\r\n" +
                    "   }\r\n" +

                    "   function ClientConfigField(AssetTypeId)\r\n"+
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideGetConfigForm(SiteId, AssetTypeId).value;\r\n"+
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "       document.getElementById('divMainForm').style.display='none';\r\n" +
                    "   }\r\n"+


                    "   function ClientConfigSearchField(AssetTypeId)\r\n" +
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideGetConfigSearchForm(SiteId, AssetTypeId).value;\r\n"+
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "       document.getElementById('divMainForm').style.display='none';\r\n" +
                    "   }\r\n"+

                    "   function ClientConfigPrintField(AssetTypeId)\r\n" +
                    "   {\r\n" +
                    "       SiteId='" + SiteId + "';\r\n" +
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideGetConfigPrintForm(SiteId, AssetTypeId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "       document.getElementById('divMainForm').style.display='none';\r\n" +
                    "   }\r\n" +

                    "   function ClientSaveAssetFieldConfig(AssetTypeId)\r\n"+
                    "   {\r\n"+
                    "       MaxFields=parseInt(document.getElementById('txtMaxFields').value);\r\n"+
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideGetSaveObject(SiteId).value;\r\n"+
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       AssetViewFields=AjaxOut.RetObject;\r\n"+
                    "       for(var iIndex=0;iIndex<MaxFields;iIndex++)\r\n"+
                    "       {\r\n"+
                    "           AssetViewFields[iIndex].Title=document.getElementById('txtTitle'+iIndex).value;\r\n" +
                    "           AssetViewFields[iIndex].SortIndex=document.getElementById('txtSortIndex'+iIndex).value;\r\n" +
                    "           Checked=document.getElementById('chkCheck'+iIndex).checked;\r\n" +
                    "           AssetViewFields[iIndex].Active=Checked?1:0;\r\n" +
                    "       }\r\n"+
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideSaveFields(SiteId, AssetTypeId, AssetViewFields).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       alert(AjaxOut.InfoMessage);\r\n" +
                    "   }\r\n"+


                    "   function ClientSaveAssetFieldSearchConfig(AssetTypeId)\r\n" +
                    "   {\r\n"+
                    "       MaxFields=parseInt(document.getElementById('txtMaxFields').value);\r\n"+
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideGetSaveObject(SiteId).value;\r\n"+
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       AssetViewFields=AjaxOut.RetObject;\r\n"+
                    "       for(var iIndex=0;iIndex<MaxFields;iIndex++)\r\n"+
                    "       {\r\n"+
                    "           AssetViewFields[iIndex].Title=document.getElementById('txtTitle'+iIndex).value;\r\n" +
                    "           AssetViewFields[iIndex].SortIndex=document.getElementById('txtSortIndex'+iIndex).value;\r\n" +
                    "           Checked=document.getElementById('chkCheck'+iIndex).checked;\r\n" +
                    "           AssetViewFields[iIndex].Active=Checked?1:0;\r\n" +
                    "       }\r\n"+
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideSaveSearchFields(SiteId, AssetTypeId, AssetViewFields).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       alert(AjaxOut.InfoMessage);\r\n" +
                    "   }\r\n"+


                    "   function ClientSaveAssetFieldPrintConfig(AssetTypeId)\r\n" +
                    "   {\r\n" +
                    "       MaxFields=parseInt(document.getElementById('txtMaxFields').value);\r\n" +
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideGetSaveObject(SiteId).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       AssetViewFields=AjaxOut.RetObject;\r\n" +
                    "       for(var iIndex=0;iIndex<MaxFields;iIndex++)\r\n" +
                    "       {\r\n" +
                    "           AssetViewFields[iIndex].Title=document.getElementById('txtTitle'+iIndex).value;\r\n" +
                    "           AssetViewFields[iIndex].SortIndex=document.getElementById('txtSortIndex'+iIndex).value;\r\n" +
                    "           Checked=document.getElementById('chkCheck'+iIndex).checked;\r\n" +
                    "           AssetViewFields[iIndex].Active=Checked?1:0;\r\n" +
                    "       }\r\n" +
                    "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AssetType.ServerSideSavePrintFields(SiteId, AssetTypeId, AssetViewFields).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n" +
                    "       {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "       }\r\n" +
                    "       alert(AjaxOut.InfoMessage);\r\n" +
                    "   }\r\n" +


                    "   function ClientCheckAll()\r\n"+
                    "   {\r\n"+
                    "       MaxFields=parseInt(document.getElementById('txtMaxFields').value);\r\n" +
                    "       for(var iIndex=0;iIndex<MaxFields;iIndex++)\r\n" +
                    "       {\r\n" +
                    "           document.getElementById('chkCheck'+iIndex).checked=true;\r\n" +
                    "       }\r\n" +
                    "   }\r\n"+

                    "   function ClientUnCheckAll()\r\n" +
                    "   {\r\n" +
                    "       MaxFields=parseInt(document.getElementById('txtMaxFields').value);\r\n" +
                    "       for(var iIndex=0;iIndex<MaxFields;iIndex++)\r\n" +
                    "       {\r\n" +
                    "           document.getElementById('chkCheck'+iIndex).checked=false;\r\n" +
                    "       }\r\n" +
                    "   }\r\n" +

                    "   function ClientInvert()\r\n" +
                    "   {\r\n" +
                    "       MaxFields=parseInt(document.getElementById('txtMaxFields').value);\r\n" +
                    "       for(var iIndex=0;iIndex<MaxFields;iIndex++)\r\n" +
                    "       {\r\n" +
                    "           document.getElementById('chkCheck'+iIndex).checked=!document.getElementById('chkCheck'+iIndex).checked;\r\n" +
                    "       }\r\n" +
                    "   }\r\n" +
                    
                    "</script>\r\n" +
                    "<div id=divActionForm></div>\r\n"+
                    "<div id=divMainForm>\r\n" +
                    "<div id=divProcessing></div>\r\n" +
                    "<div id=\"divAssetTypeContent\" class=\"tabcontent\">" + ServerSideReading(loadParamCls.SiteId) + "</div>\r\n" +
                    "</div>\r\n";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        

        [AjaxPro.AjaxMethod]
        public string ServerSideReading(string SiteId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                AssetTypeCls[] AssetTypes = AssetBll.ReadingAssetType(SiteId);
                if (AssetTypes.Length == 0)
                {
                    return "<div class=CssNoData>Không có dữ liệu thỏa mãn điều kiện lọc</div>\r\n";
                }
                string
                    Html =
                           "<div class=CssRecordFound>Tổng số biểu ghi dữ liệu " + AssetTypes.Length.ToString("#,##0") + "</div>\r\n" +
                           "<table>\r\n" +
                           "  <tr>\r\n" +
                           "      <th class=\"thIndex\">#</th>" +
                           "      <th class=\"thIcon\"></th>" +
                           "      <th style=\"width:150px;\">Mã</th>\r\n" +
                           "      <th>Loại hiện vật</th>\r\n" +
                           "      <th>Icon</th>\r\n" +
                           "      <th class=thFunction></th>\r\n" +
                           " </tr>\r\n";
                for (int iIndex = 0; iIndex < AssetTypes.Length; iIndex++)
                {
                    string Ids = "Item" + iIndex;
                    string ButtonText = FunctionButton.GetButtonItems(new ButtonItem[]{
                            new UpdateButtonItem(SiteId, "update","Cấu hình các trường dữ liệu nhập liệu","javascript:ClientConfigField('" + AssetTypes[iIndex].AssetTypeId + "');"),
                            new ButtonItem("configsearch","Cấu hình các trường dữ liệu tìm kiếm",Environments.GetTemplateFolder(SiteId)+"/images/modules/search-small.png","javascript:ClientConfigSearchField('" + AssetTypes[iIndex].AssetTypeId + "');"),
                            new ButtonItem("configprint","Cấu hình các trường dữ liệu tìm kiếm để in sổ",Environments.GetTemplateFolder(SiteId)+"/images/modules/print-small.png","javascript:ClientConfigPrintField('" + AssetTypes[iIndex].AssetTypeId + "');"),
                        });

                    Html +=
                           "  <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">\r\n" +
                           "      <td class=\"tdIndex\">" + (iIndex + 1) + "</td>" +
                           "      <td class=tdIcon><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/small-office.png\"></td>\r\n" +
                           "      <td>" + AssetTypes[iIndex].AssetTypeId + "</td>\r\n" +
                           "      <td>" + AssetTypes[iIndex].AssetTypeName + "</td>\r\n" +
                           "      <td>" + AssetTypes[iIndex].Icon + "</td>\r\n" +
                           "      <td class=tdFunction>" + ButtonText + "</td>\r\n" +
                           " </tr>\r\n";
                }
                Html += "</table>\r\n";
                return Html;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideGetSaveObject(string SiteId)
        {
            AjaxReturn OAjaxReturn=new AjaxReturn();
            try
            {
                System.Collections.ObjectModel.Collection<AssetViewField>
                    AssetViewFields = AssetViewFieldBll.GetAssetViewFields(SiteId);
                AssetViewField[] RetAssetViewFields = new AssetViewField[AssetViewFields.Count];
                for (int iIndex = 0; iIndex < AssetViewFields.Count; iIndex++)
                {
                    RetAssetViewFields[iIndex] = AssetViewFields[iIndex];
                }
                OAjaxReturn.RetObject = RetAssetViewFields;
            }
            catch (Exception ex)
            {
                OAjaxReturn.InfoMessage = ex.Message.ToString();
                OAjaxReturn.Error = true;
            }
            return OAjaxReturn;
        }



        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideSaveFields(string SiteId,string AssetTypeId, AssetViewField[] AssetViewFields)
        {
            AjaxReturn OAjaxReturn = new AjaxReturn();
            try
            {
                AssetBll.SaveAssetTypeFieldConfig(SiteId, AssetTypeId, AssetViewFields);
                OAjaxReturn.InfoMessage = "Ghi lại cấu hình hiển thị thành công";
            }
            catch (Exception ex)
            {
                OAjaxReturn.InfoMessage = ex.Message.ToString();
                OAjaxReturn.Error = true;
            }
            return OAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideSaveSearchFields(string SiteId, string AssetTypeId, AssetViewField[] AssetViewFields)
        {
            AjaxReturn OAjaxReturn = new AjaxReturn();
            try
            {
                AssetBll.SaveAssetTypeSearchFieldConfig(SiteId, AssetTypeId, AssetViewFields);
                OAjaxReturn.InfoMessage = "Ghi lại cấu hình tìm kiếm thành công";
            }
            catch (Exception ex)
            {
                OAjaxReturn.InfoMessage = ex.Message.ToString();
                OAjaxReturn.Error = true;
            }
            return OAjaxReturn;
        }


        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideSavePrintFields(string SiteId, string AssetTypeId, AssetViewField[] AssetViewFields)
        {
            AjaxReturn OAjaxReturn = new AjaxReturn();
            try
            {
                AssetBll.SaveAssetTypePrintFieldConfig(SiteId, AssetTypeId, AssetViewFields);
                OAjaxReturn.InfoMessage = "Ghi lại cấu hình in sổ thành công";
            }
            catch (Exception ex)
            {
                OAjaxReturn.InfoMessage = ex.Message.ToString();
                OAjaxReturn.Error = true;
            }
            return OAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideGetConfigForm(string SiteId, string AssetTypeId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                System.Collections.ObjectModel.Collection<AssetViewField>  AssetViewFields=
                    AssetViewFieldBll.GetAssetViewFields(SiteId);
                AssetTypeCls OAssetTypeCls = AssetBll.CreateAssetTypeModel(SiteId, AssetTypeId, true);
                DataSet ds = null;
                if (!string.IsNullOrEmpty(OAssetTypeCls.XmlFieldConfig))
                {
                    ds = new DataSet();
                    ds.ReadXml(new StringReader(OAssetTypeCls.XmlFieldConfig));
                }

                string Html =
                    "<input type=hidden id=\"txtMaxFields\" value=\"" + AssetViewFields.Count+ "\">\r\n" +
                    " <div class=tabfunction>\r\n"+
                    "       <input type=button class=button value=\"Đánh dấu tất\" onclick=\"javascript:ClientCheckAll();\">\r\n"+
                    "       <input type=button class=button value=\"Bỏ đánh dấu\" onclick=\"javascript:ClientUnCheckAll();\">\r\n" +
                    "       <input type=button class=button value=\"Đánh dấu ngược\" onclick=\"javascript:ClientInvert();\">\r\n" +
                    "       <input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveAssetFieldConfig('"+AssetTypeId+"');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\">\r\n"+
                    " </div>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "       <tr>\r\n" +
                    "       <th class=\"thIndex\">#</th>" +
                    //"       <th class=\"thIcon\"></th>" +
                    "       <th></th>" +
                    "       <th>Tên gốc</th>" +
                    "       <th>Tên hiện thị</th>" +
                    "       <th>Thứ tự</th>";
                for (int iIndex = 0; iIndex < AssetViewFields.Count; iIndex++)
                {
                    string ItemTitle = AssetViewFields[iIndex].Title;
                    string SortIndex = AssetViewFields[iIndex].SortIndex;
                    int Active = AssetViewFields[iIndex].Active;
                    string Checked = "";
                    if (ds != null)
                    {
                        DataView dv=new DataView(ds.Tables[0],"Id='"+AssetViewFields[iIndex].Id+"'","",DataViewRowState.CurrentRows);

                        if (dv.Count > 0)
                        {
                            try
                            {
                                ItemTitle = dv[0]["Title"].ToString();
                            }
                            catch { }
                            try
                            {
                                SortIndex = dv[0]["SortIndex"].ToString();
                            }
                            catch { }
                            try
                            {
                                Active = int.Parse(dv[0]["Active"].ToString());
                            }
                            catch { }
                            if (Active == 1)
                            {
                                Checked = " CHECKED ";
                            }
                        }
                    }
                    string Css = "style=\"color:maroon;\" ";
                    if (Active == 0)
                    {
                        Css = " style=\"color:silver;\" ";
                    }
                    Html+=
                         "  <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">\r\n" +
                           "      <td class=\"tdIndex\" style=\"vertical-align:top;\">" + (iIndex + 1) + "</td>" +
                           "      <td style=\"width:22px;text-align:center;vertical-align:top\"><input id=\"chkCheck" + iIndex + "\" " + Checked + " type=checkbox></td>\r\n" +
                           "      <td style=\"text-align:right;margin-right:5px;\"><span " + Css + ">" + AssetViewFields[iIndex].Title + "<br>" + AssetViewFields[iIndex].SqlField + "</span></td>\r\n" +
                           "      <td style=\"margin-left:5px;vertical-align:top\"><input id=\"txtTitle"+iIndex+"\" "+Css+" type=textbox class=textbox value=\"" + ItemTitle + "\"></td>\r\n" +
                           "      <td style=\"width:80px;text-align:center;vertical-align:top\"><input id=\"txtSortIndex" + iIndex + "\" style=\"width:40px;text-align:center;\" type=textbox class=textbox " + Css + " value=\"" + SortIndex + "\"></td>\r\n" +
                           " </tr>\r\n";
                }
                Html += "</table>\r\n";
                    
                oAjaxReturn.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error=true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }







        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideGetConfigSearchForm(string SiteId, string AssetTypeId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                System.Collections.ObjectModel.Collection<AssetViewField> AssetViewFields =
                    AssetViewFieldBll.GetAssetViewFields(SiteId);
                AssetTypeCls OAssetTypeCls = AssetBll.CreateAssetTypeModel(SiteId, AssetTypeId, true);
                DataSet ds = null;
                if (!string.IsNullOrEmpty(OAssetTypeCls.XmlConfigSearch))
                {
                    ds = new DataSet();
                    ds.ReadXml(new StringReader(OAssetTypeCls.XmlConfigSearch));
                }

                string Html =
                    "<input type=hidden id=\"txtMaxFields\" value=\"" + AssetViewFields.Count + "\">\r\n" +
                    " <div class=tabfunction>\r\n" +
                    "       <input type=button class=button value=\"Đánh dấu tất\" onclick=\"javascript:ClientCheckAll();\">\r\n" +
                    "       <input type=button class=button value=\"Bỏ đánh dấu\" onclick=\"javascript:ClientUnCheckAll();\">\r\n" +
                    "       <input type=button class=button value=\"Đánh dấu ngược\" onclick=\"javascript:ClientInvert();\">\r\n" +
                    "       <input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveAssetFieldSearchConfig('" + AssetTypeId + "');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\">\r\n" +
                    " </div>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "       <tr>\r\n" +
                    "       <th class=\"thIndex\">#</th>" +
                    //"       <th class=\"thIcon\"></th>" +
                    "       <th></th>" +
                    "       <th>Tên gốc</th>" +
                    "       <th>Tên hiện thị</th>" +
                    "       <th>Thứ tự</th>";
                for (int iIndex = 0; iIndex < AssetViewFields.Count; iIndex++)
                {
                    string ItemTitle = AssetViewFields[iIndex].Title;
                    string SortIndex = AssetViewFields[iIndex].SortIndex;
                    int Active = AssetViewFields[iIndex].Active;
                    string Checked = "";
                    if (ds != null)
                    {
                        DataView dv = new DataView(ds.Tables[0], "Id='" + AssetViewFields[iIndex].Id + "'", "", DataViewRowState.CurrentRows);

                        if (dv.Count > 0)
                        {
                            try
                            {
                                ItemTitle = dv[0]["Title"].ToString();
                            }
                            catch { }
                            try
                            {
                                SortIndex = dv[0]["SortIndex"].ToString();
                            }
                            catch { }
                            try
                            {
                                Active = int.Parse(dv[0]["Active"].ToString());
                            }
                            catch { }
                            if (Active == 1)
                            {
                                Checked = " CHECKED ";
                            }
                        }
                    }
                    string Css = "style=\"color:maroon;\" ";
                    if (Active == 0)
                    {
                        Css = " style=\"color:silver;\" ";
                    }
                    Html +=
                         "  <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">\r\n" +
                           "      <td class=\"tdIndex\">" + (iIndex + 1) + "</td>" +
                        //"      <td class=tdIcon><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/small-office.png\"></td>\r\n" +
                           "      <td style=\"width:22px;text-align:center;\"><input id=\"chkCheck" + iIndex + "\" " + Checked + " type=checkbox></td>\r\n" +
                           "      <td style=\"text-align:right;margin-right:5px;\"><span " + Css + ">" + AssetViewFields[iIndex].Title + "</span></td>\r\n" +
                           "      <td style=\"margin-left:5px;\"><input id=\"txtTitle" + iIndex + "\" " + Css + " type=textbox class=textbox value=\"" + ItemTitle + "\"></td>\r\n" +
                           "      <td style=\"width:80px;text-align:center;\"><input id=\"txtSortIndex" + iIndex + "\" style=\"width:40px;text-align:center;\" type=textbox class=textbox " + Css + " value=\"" + SortIndex + "\"></td>\r\n" +
                           " </tr>\r\n";
                }
                Html += "</table>\r\n";

                oAjaxReturn.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }



        [AjaxPro.AjaxMethod]
        public AjaxReturn ServerSideGetConfigPrintForm(string SiteId, string AssetTypeId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                System.Collections.ObjectModel.Collection<AssetViewField> AssetViewFields =
                    AssetViewFieldBll.GetAssetViewFields(SiteId);
                AssetTypeCls OAssetTypeCls = AssetBll.CreateAssetTypeModel(SiteId, AssetTypeId, true);
                DataSet ds = null;
                if (!string.IsNullOrEmpty(OAssetTypeCls.XmlConfigPrint))
                {
                    ds = new DataSet();
                    ds.ReadXml(new StringReader(OAssetTypeCls.XmlConfigPrint));
                }

                string Html =
                    "<input type=hidden id=\"txtMaxFields\" value=\"" + AssetViewFields.Count + "\">\r\n" +
                    " <div class=tabfunction>\r\n" +
                    "       <input type=button class=button value=\"Đánh dấu tất\" onclick=\"javascript:ClientCheckAll();\">\r\n" +
                    "       <input type=button class=button value=\"Bỏ đánh dấu\" onclick=\"javascript:ClientUnCheckAll();\">\r\n" +
                    "       <input type=button class=button value=\"Đánh dấu ngược\" onclick=\"javascript:ClientInvert();\">\r\n" +
                    "       <input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveAssetFieldPrintConfig('" + AssetTypeId + "');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientBack();\">\r\n" +
                    " </div>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "       <tr>\r\n" +
                    "       <th class=\"thIndex\">#</th>" +
                    //"       <th class=\"thIcon\"></th>" +
                    "       <th></th>" +
                    "       <th>Tên gốc</th>" +
                    "       <th>Tên hiện thị</th>" +
                    "       <th>Thứ tự</th>";
                for (int iIndex = 0; iIndex < AssetViewFields.Count; iIndex++)
                {
                    string ItemTitle = AssetViewFields[iIndex].Title;
                    string SortIndex = AssetViewFields[iIndex].SortIndex;
                    int Active = AssetViewFields[iIndex].Active;
                    string Checked = "";
                    if (ds != null)
                    {
                        DataView dv = new DataView(ds.Tables[0], "Id='" + AssetViewFields[iIndex].Id + "'", "", DataViewRowState.CurrentRows);

                        if (dv.Count > 0)
                        {
                            try
                            {
                                ItemTitle = dv[0]["Title"].ToString();
                            }
                            catch { }
                            try
                            {
                                SortIndex = dv[0]["SortIndex"].ToString();
                            }
                            catch { }
                            try
                            {
                                Active = int.Parse(dv[0]["Active"].ToString());
                            }
                            catch { }
                            if (Active == 1)
                            {
                                Checked = " CHECKED ";
                            }
                        }
                    }
                    string Css = "style=\"color:maroon;\" ";
                    if (Active == 0)
                    {
                        Css = " style=\"color:silver;\" ";
                    }
                    Html +=
                         "  <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">\r\n" +
                           "      <td class=\"tdIndex\">" + (iIndex + 1) + "</td>" +
                        //"      <td class=tdIcon><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/small-office.png\"></td>\r\n" +
                           "      <td style=\"width:22px;text-align:center;\"><input id=\"chkCheck" + iIndex + "\" " + Checked + " type=checkbox></td>\r\n" +
                           "      <td style=\"text-align:right;margin-right:5px;\"><span " + Css + ">" + AssetViewFields[iIndex].Title + "</span></td>\r\n" +
                           "      <td style=\"margin-left:5px;\"><input id=\"txtTitle" + iIndex + "\" " + Css + " type=textbox class=textbox value=\"" + ItemTitle + "\"></td>\r\n" +
                           "      <td style=\"width:80px;text-align:center;\"><input id=\"txtSortIndex" + iIndex + "\" style=\"width:40px;text-align:center;\" type=textbox class=textbox " + Css + " value=\"" + SortIndex + "\"></td>\r\n" +
                           " </tr>\r\n";
                }
                Html += "</table>\r\n";

                oAjaxReturn.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }


        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AssetType), ActivePage);
        }
    }
}
